package com.kmproject.haifu.mvp.ui.holder;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseParmarBean;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectAddress1Adapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectAddress2Adapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectAddressStrAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressSelectHolder {

    @BindView(R.id.popuwindows_seelct_adress_qy)
    TextView popuwindowsSeelctAdressQy;
    @BindView(R.id.popuwindows_seelct_adress_fj)
    TextView popuwindowsSeelctAdressFj;
    @BindView(R.id.popuwindows_seelct_adress_RecyclerView)
    RecyclerView popuwindowsSeelctAdressRecyclerView;
    @BindView(R.id.popuwindows_seelct_adress_RecyclerView1)
    RecyclerView popuwindowsSeelctAdressRecyclerView1;
    @BindView(R.id.popuwindows_seelct_adress_lay)
    LinearLayout popuwindowsSeelctAdressLay;
    @BindView(R.id.popuwindows_seelct_adress_fjRecyclerView)
    RecyclerView popuwindowsSeelctAdressFjRecyclerView;
    private PopuWindowSelectAddress1Adapter popuWindowSelectAddress1;
    private Context mcontext;
    List<HousePayWayBean> maddressSelectData = null;
    List<HousePayWayBean> maddressSelectDataDis = null;
    private PopuWindowSelectAddress2Adapter popuWindowSelectAddress2Adapter;
    private PopuWindowSelectAddressStrAdapter popuWindowSelectAddress3Adapter;
    private HousePayWayBean mdefaultHousePayWayBean;//默认选中的地址
    public AddressSelectHolder(Context context, View view, List<HousePayWayBean> addressSelectData, List<HousePayWayBean> addRessHousePayWayBeansDis,HousePayWayBean defaultHousePayWayBean) {
//        R.layout.popuwindows_seelct_adress
        ButterKnife.bind(this, view);
        mdefaultHousePayWayBean=defaultHousePayWayBean;
        mcontext = context;
        if (addressSelectData != null && !addressSelectData.get(0).getName().equals("不限")) {
            HousePayWayBean housePayWayBean = new HousePayWayBean("不限");
            housePayWayBean.setName("不限");
            addressSelectData.add(0, housePayWayBean);
        }
        maddressSelectData = addressSelectData;
        if (addRessHousePayWayBeansDis != null && !addRessHousePayWayBeansDis.get(0).equals("不限")) {
            HousePayWayBean housePayWayBean1 = new HousePayWayBean("不限");
            housePayWayBean1.setName("不限");
            addRessHousePayWayBeansDis.add(0, housePayWayBean1);
        }
        maddressSelectDataDis = addRessHousePayWayBeansDis;
    }

    public void init(SelectAddressCallBack selectAddressCallBack) {
        showContarl(0);
        if (maddressSelectData == null || maddressSelectData.isEmpty()) {
            ArmsUtils.makeText(mcontext, "未获取到区域信息");
            return;
        }
        int[] defaultSelectIndexs=getdefaultSelectAddress(mdefaultHousePayWayBean,maddressSelectData);
        //设置第二个
        popuwindowsSeelctAdressRecyclerView1.setLayoutManager(new LinearLayoutManager(mcontext));
        popuWindowSelectAddress2Adapter = new PopuWindowSelectAddress2Adapter(defaultSelectIndexs[1],new ArrayList<>(), new PopuWindowSelectAddress2Adapter.BtncliickCallBack() {
            @Override
            public void callBack(int index, Object content) {
                //回调哥界面
                selectAddressCallBack.callBack("区域", index, (HousePayWayBean) content);
            }
        });
        popuwindowsSeelctAdressRecyclerView1.setAdapter(popuWindowSelectAddress2Adapter);
        popuWindowSelectAddress2Adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);

        //设置第一个
        popuwindowsSeelctAdressRecyclerView.setLayoutManager(new LinearLayoutManager(mcontext));
        popuWindowSelectAddress1 = new PopuWindowSelectAddress1Adapter(defaultSelectIndexs[0],maddressSelectData, new PopuWindowSelectAddress1Adapter.BtncliickCallBack() {
            @Override
            public void callBack(int index, Object content) {
                if (!maddressSelectData.get(index).getName().equals("不限") && !maddressSelectData.get(index).getChildren().get(0).getName().equals("不限")) {
                    HousePayWayBean housePayWayBean = new HousePayWayBean("不限");
                    housePayWayBean.setName("不限");
                    maddressSelectData.get(index).getChildren().add(0, housePayWayBean);
                    popuWindowSelectAddress2Adapter.setNewData(maddressSelectData.get(index).getChildren());
                } else {
                    //第一级选着了不限
                    selectAddressCallBack.callBack("区域", index, (HousePayWayBean) content);
                }

            }
        });
        popuwindowsSeelctAdressRecyclerView.setAdapter(popuWindowSelectAddress1);
        popuWindowSelectAddress1.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);


        popuwindowsSeelctAdressFjRecyclerView.setLayoutManager(new LinearLayoutManager(mcontext));
        popuWindowSelectAddress3Adapter = new PopuWindowSelectAddressStrAdapter(maddressSelectDataDis, new PopuWindowSelectAddressStrAdapter.BtncliickCallBack() {
            @Override
            public void callBack(int index, Object content) {
                selectAddressCallBack.callBack("范围", index, content);
            }
        });
        popuwindowsSeelctAdressFjRecyclerView.setAdapter(popuWindowSelectAddress3Adapter);
        popuWindowSelectAddress3Adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
    }

    @OnClick({R.id.popuwindows_seelct_adress_qy, R.id.popuwindows_seelct_adress_fj})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.popuwindows_seelct_adress_qy:
                showContarl(0);
                break;
            case R.id.popuwindows_seelct_adress_fj:
                showContarl(1);
                break;
        }
    }

    /**
     * 控制布局显示隐藏
     *
     * @param type
     */
    public void showContarl(int type) {
        if (type == 0) {
            popuwindowsSeelctAdressLay.setVisibility(View.VISIBLE);
            popuwindowsSeelctAdressFjRecyclerView.setVisibility(View.GONE);
            popuwindowsSeelctAdressQy.setBackgroundColor(ArmsUtils.getColor(mcontext, R.color.white));
            popuwindowsSeelctAdressQy.setTextColor(ArmsUtils.getColor(mcontext, R.color.color_747474));
            popuwindowsSeelctAdressFj.setBackgroundColor(ArmsUtils.getColor(mcontext, R.color.color_EDEDED));
            popuwindowsSeelctAdressFj.setTextColor(ArmsUtils.getColor(mcontext, R.color.color_191919));
            popuwindowsSeelctAdressQy.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            popuwindowsSeelctAdressFj.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        } else {
            popuwindowsSeelctAdressLay.setVisibility(View.GONE);
            popuwindowsSeelctAdressFjRecyclerView.setVisibility(View.VISIBLE);
            popuwindowsSeelctAdressQy.setBackgroundColor(ArmsUtils.getColor(mcontext, R.color.color_EDEDED));
            popuwindowsSeelctAdressQy.setTextColor(ArmsUtils.getColor(mcontext, R.color.color_191919));
            popuwindowsSeelctAdressFj.setBackgroundColor(ArmsUtils.getColor(mcontext, R.color.white));
            popuwindowsSeelctAdressFj.setTextColor(ArmsUtils.getColor(mcontext, R.color.color_747474));
            popuwindowsSeelctAdressFj.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            popuwindowsSeelctAdressQy.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
    }

    /**
     * 得到默认选中的地址
     * @param defaultHousePayWayBean
     * @param housePayWayBeans
     * @return
     */
    public int[]  getdefaultSelectAddress(HousePayWayBean defaultHousePayWayBean,List<HousePayWayBean> housePayWayBeans)
    {
        int[] selectIds=new int[]{0,0};
        if (defaultHousePayWayBean==null||housePayWayBeans==null||housePayWayBeans.isEmpty())
        {
            return selectIds;
        }
        for (int i=0;i<housePayWayBeans.size();i++)
        {
            if (housePayWayBeans.get(i).getParentId()!=null&&defaultHousePayWayBean.getParentId()!=null&&defaultHousePayWayBean.getParentId().equals(housePayWayBeans.get(i).getParentId()))
            {
                selectIds[0]=i;
                for (int k=0;k<housePayWayBeans.size();k++)
                {
                    if (housePayWayBeans.get(k).getDictId()!=null&&defaultHousePayWayBean.getDictId()!=null&&defaultHousePayWayBean.getDictId().equals(housePayWayBeans.get(k).getDictId()))
                    {
                        selectIds[1]=k;
                    }
                }
            }
        }
        return selectIds;
    }
    public interface SelectAddressCallBack {
        void callBack(String type, int index, Object o);
    }
}
